-- bonus
-- create by panyl
-- 道具：  奖励

return {
    apply = function(source, target, classId, value)
        -- 转化道具
        local function change_item(user, arr)
            local src_id = arr[1];
            local dst_id = arr[2];
            local count = ItemM.getAmount(user, src_id);

            if count > 0 then
                ItemM.costAmount(user, src_id, count);
            end

            -- 如果身上有
            local equipType = EquipM.query(src_id, "type");
            local equipments = user.equipments;
            local equiped = equipments[equipType] or {};
            if equiped[1] == src_id then
                count = count + 1;

                -- 扣除
                equipments[equipType] = nil;

                -- 刷新属性
                PropM.refreshEffect(user, "equip");
            end

            -- 赋予一个属性
            local prop = { PropM.getPropId("item_exchange"), src_id, dst_id, -1, };
            PropM.record(user, prop, "property_forever");

            -- 奖励
            BonusM.doBonus({1, dst_id, count, }, "item_exchange");
        end

        -- 转换单件
        if type(value[1]) == "number" then
            -- 扣除旧物品，扣除成功则奖励新物品
            change_item(source, value);
        elseif type(value[1]) == "table" then
            for _, arr in ipairs(value) do
                -- 扣除旧物品，扣除成功则奖励新物品
                change_item(source, arr);
            end
        end
    end,
};
